package servlet;

import user.ShopCar;
import util.DBUtilDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class SelectShopCarServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        List<ShopCar> shopCar = null;
        float result = 0;
        DBUtilDao dbUtilDao = new DBUtilDao();

        try {
            shopCar = dbUtilDao.getShopCar(username);//根据手机号，查询数据库中的购物车数据表
            session.setAttribute("shopCar", shopCar); //购物车
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        for (ShopCar shopCar1 : shopCar) {//遍历list集合，计算出总价
            result = result + shopCar1.getTotalPrice();
            session.setAttribute("result", result);//购物车中所有物品总价
        }

        try {
            dbUtilDao.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
